﻿using AutoMapper;
using B.MES.SLY.Domain.Entities;
using B.MES.SLY.Service.Input.Bom;
using B.MES.SLY.Service.Input.Customer;
using B.MES.SLY.Service.Input.Material;
using B.MES.SLY.Service.Input.ProductionLine;
using B.MES.SLY.Service.Input.Record;
using B.MES.SLY.Service.Input.Supplier;
using B.MES.SLY.Service.Input.WorkShop;
using B.MES.SLY.Service.Input.WorkStation;
using B.MES.SLY.Service.OutPut;
using Dm.util;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Standard;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Runtime.CompilerServices;
using Yitter.IdGenerator;
using static Dm.net.buffer.ByteArrayBuffer;
using BomMaterialIdModel = B.MES.SLY.Domain.Entities.BomMaterialIdModel;
using BomModel = B.MES.SLY.Domain.Entities.BomModel;
using Parent_ChildrenModel = B.MES.SLY.Domain.Entities.Parent_ChildrenModel;
namespace B.MES.SLY.Service
{
    public class MesService : IMesService
    {
       
        private readonly MesRepository<SLY_WORKSHOP_MANAGEMENTMODEL> _workShowRepo;
        private readonly MesRepository<SLY_PRODUCTIONLINE_MANAGEMENTMODEL> _productionlineRepo;
        private readonly  MesRepository<SLY_WORKSTATION_MANAGEMENTMODEL> _workStationRepo;
        private readonly MesRepository<SLY_CUSTOMER_MANAGEMENTMODEL> _customerRepo;
        private readonly MesRepository<SLY_SUPPLIER_MANAGEMENTMODEL> _supplierRepo;
        private readonly MesRepository<SLY_COLLECTION_RECORD> _collectionRepo;
        private readonly MesRepository<SLY_DELIVERY_RECORD> _deliveryRepo;
        private readonly MesRepository<SLY_INVOICE_RECORD> _involcenRepo;
        private readonly MesRepository<SLY_SALES_RECORDMODEL> _salesnRepo;
        private readonly MesRepository<SLY_RELATION_RECORDMODEL> _relationRepo;
        private readonly MesRepository<BomMaterialIdModel> _materialRepo;
        private readonly MesRepository<BomModel> _bomRepo;
        //private readonly MesRepository<Parent_ChildrenModel> _pcRepo;
        private readonly IMapper _mapper;
        private readonly ISqlSugarClient sqlSugarClient;
        private readonly MesRepository<Parent_ChildrenModel> PcRepository;
        public MesService(MesRepository<SLY_WORKSHOP_MANAGEMENTMODEL> workShowRepo, MesRepository<SLY_PRODUCTIONLINE_MANAGEMENTMODEL> productionlineRepo, MesRepository<SLY_WORKSTATION_MANAGEMENTMODEL> workStationRepo, MesRepository<SLY_CUSTOMER_MANAGEMENTMODEL> customerRepo, MesRepository<SLY_SUPPLIER_MANAGEMENTMODEL> supplierRepo, IMapper mapper, ISqlSugarClient sqlSugarClient, MesRepository<SLY_COLLECTION_RECORD> collectionRepo, MesRepository<SLY_DELIVERY_RECORD> deliveryRepo, MesRepository<SLY_INVOICE_RECORD> involcenRepo, MesRepository<SLY_SALES_RECORDMODEL> salesnRepo, MesRepository<SLY_RELATION_RECORDMODEL> relationRepo, MesRepository<BomMaterialIdModel> materialRepo, MesRepository<BomModel> bomRepo, MesRepository<Parent_ChildrenModel> pcRepository)
        {
            _workShowRepo = workShowRepo;
            _productionlineRepo = productionlineRepo;
            _workStationRepo = workStationRepo;
            _customerRepo = customerRepo;
            _supplierRepo = supplierRepo;
            _mapper = mapper;
            this.sqlSugarClient = sqlSugarClient;
            _collectionRepo = collectionRepo;
            _deliveryRepo = deliveryRepo;
            _involcenRepo = involcenRepo;
            _salesnRepo = salesnRepo;
            _relationRepo = relationRepo;
            _materialRepo = materialRepo;
            _bomRepo = bomRepo;
            PcRepository = pcRepository;
        }
        #region
        /// <summary>
        /// 添加车间管理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> Add(AddWorkshopnput input)
        {
            var result = new ApiResult<int>();
            var data = _workShowRepo.GetQueryable(x => x.WORKSHOP_NAME == input.WORKSHOP_NAME).First();
            if (data != null)
            {
                result.Code = ApiCode.ParameterError;
                result.Message = "车间名称已存在";
                return result;
            }
            var model = _mapper.Map<SLY_WORKSHOP_MANAGEMENTMODEL>(input);
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            model.CREATETIME = DateTime.Now;
            model.CREATEDUSERNAME = "admin"; // 假设创建者为admin
            int r = _workShowRepo.Add(model);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.ResultData = r;
                result.Message = "添加成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "添加失败";
                return result;
            }
        }
        /// <summary>
        /// 删除车间管理
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ApiResult<int> Delete(string Id)
        {
            var result = new ApiResult<int>();
            var data = _workShowRepo.GetList().Where(x => x.ID == Id).FirstOrDefault();
            data.ISDELETE = true;
            var r = _workShowRepo.Update(data);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "删除成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "删除失败";
                return result;
            }
        }
        /// <summary>
        /// 查询所有车间管理
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<SLY_WORKSHOP_MANAGEMENTMODEL>> GetAll()
        {
            var data = _workShowRepo.GetList().Where(x => x.ISDELETE == false).ToList();
            var result = new ApiResult<List<SLY_WORKSHOP_MANAGEMENTMODEL>>
            {
                Code = ApiCode.Success,
                ResultData = data,
                Message = "查询成功"
            };
            return result;
        }
        /// <summary>
        /// 分页查询车间管理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiPaging<GetWorkShopOutPut> GetAll(SearchWorkShopInput input)
        {
            var data = _workShowRepo.GetQueryable(x => x.ISDELETE == false);
            if (!string.IsNullOrEmpty(input.WORKSHOP_CODE))
            {
                data = data.Where(x => x.WORKSHOP_CODE.Contains(input.WORKSHOP_CODE));
            }
            if (!string.IsNullOrEmpty(input.WORKSHOP_NAME))
            {
                data = data.Where(x => x.WORKSHOP_NAME.Contains(input.WORKSHOP_NAME));
            }

            var count = data.Count();
            var pagedata = data.OrderByDescending(x => x.ID).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var list = _mapper.Map<List<GetWorkShopOutPut>>(pagedata);
            var result = new ApiPaging<GetWorkShopOutPut>
            {
                Code = ApiCode.Success,
                PageDatas = list,
                Message = "查询成功",
                TotalCount = count,
                TotalPage = (int)Math.Ceiling(count / (double)input.PageSize)
            };
            return result;
        }
        /// <summary>
        /// 修改车间管理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> Update(UpdateWorkShopInput input)
        {
            var result = new ApiResult<int>();
            var data = _workShowRepo.GetEntity(x => x.ID == input.ID);
            if (data == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "车间不存在";
                return result;
            }
            var list = _mapper.Map<SLY_WORKSHOP_MANAGEMENTMODEL>(input);
            list.UPDATETIME = DateTime.Now;
            list.CREATETIME = data.CREATETIME;
            list.CREATEDUSERNAME= data.CREATEDUSERNAME; // 保持创建者不变
            list.UPDATEDUSERNAME= "admin"; // 假设更新者为admin
            int r = _workShowRepo.Update(list);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "修改成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "修改失败";
                return result;
            }
        }
        /// <summary>
        /// 批量删除车间管理
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<string> DeleteList(List<string> Ids)
        {
            var result = new ApiResult<string>();
            // 检查是否有记录需要删除
            if (Ids.Count == 0)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "未找到要删除的记录";
                return result;
            }
            // 查询需要删除的记录
            var dataList = _workShowRepo.GetList();
            int r = 0;
            foreach (var id in Ids)
            {
                var data = dataList.FirstOrDefault(x => x.ID == id.ToString() && !x.ISDELETE);
                data.ISDELETE = true;

                r = _workShowRepo.Update(data);
            }
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "批量删除成功";
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "批量删除失败";
            }

            return result;
        }
        /// <summary>
        /// 添加产线管理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddProductionLine(AddProductionLineInput input)
        {
            var result = new ApiResult<int>();
            var isExist = _productionlineRepo.GetQueryable(x => x.LINE_CODE == input.LINE_CODE);
            if (isExist.Any())
            {
                result.Code = ApiCode.ParameterError;
                result.Message = "产线编码已存在";
                return result;
            }
            var list = _mapper.Map<SLY_PRODUCTIONLINE_MANAGEMENTMODEL>(input);
            list.CREATEDUSERNAME = "admin";
            list.CREATETIME = DateTime.Now;
            list.ISDELETE = false;
            list.ID = YitIdHelper.NextId().ToString();
            var r = _productionlineRepo.Add(list);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "添加成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "添加失败";
                return result;
            }

        }
        /// <summary>
        /// 查询产线管理列表
        /// </summary>
        public ApiResult<List<GetProductionLineOutPut>> GetProductionLine()
        {
            var list = _mapper.Map<List<GetProductionLineOutPut>>(_productionlineRepo.GetList());
            foreach (var item in list)
            {
               
                var name = _workShowRepo.GetEntity(x => x.ID == item.WORKSHOP_ID);
                if (name != null)
                {
                    item.WORKSHOP_NAME = name.WORKSHOP_NAME;
                }
            }
            var result = new ApiResult<List<GetProductionLineOutPut>>
            {
                Code = ApiCode.Success,
                Message = "查询成功",
                ResultData = list
            };
            return result;
        }
        /// <summary>
        /// 修改产线管理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateProductionLine(UpdateProductionLineInput input)
        {
            var result = new ApiResult<int>();
            var data = _productionlineRepo.GetEntity(x => x.ID == input.ID);
            if (data == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "产线不存在";
                return result;
            }
            var list = _mapper.Map<SLY_PRODUCTIONLINE_MANAGEMENTMODEL>(input);
            list.UPDATETIME = DateTime.Now;
            list.CREATETIME = data.CREATETIME; // 保持创建时间不变
            list.CREATETIME= data.CREATETIME;
            list.UPDATEDUSERNAME = "admin";
            list.CREATEDUSERNAME =data.CREATEDUSERNAME;
            int r = _productionlineRepo.Update(list);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "修改成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "修改失败";
                return result;
            }
        }
        /// <summary>
        /// 删除产线管理
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> DeleteProductionLine(string Id)
        {
            var result = new ApiResult<int>();
            var isExist = _productionlineRepo.GetEntity(x => x.ID == Id);
            if (isExist == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "产线不存在";
                return result;
            }
            isExist.ISDELETE = true;
            var r = _productionlineRepo.Update(isExist);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "删除成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "删除失败";
                return result;
            }
        }
        /// <summary>
        /// 批量删除产线管理
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<string> DeleteListProductionLine(List<string> Ids)
        {
            var result = new ApiResult<string>();
            int r = 0;
            foreach (var item in Ids)
            {
                var dataList = _productionlineRepo.GetList().Where(x => x.ID == item).First();
                dataList.ISDELETE = true;
                r = _productionlineRepo.Update(dataList);
            }
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "批量删除成功";
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "批量删除失败";
            }
            return result;
        }
        /// <summary>
        /// 查询产线管理分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<GetProductionLineOutPut> GetPageProductionLine(SearchProductionLineInput input)
        {
            var result = new ApiPaging<GetProductionLineOutPut>();
            var model = _productionlineRepo.GetQueryable(x => x.ISDELETE == false);
            var WorkShop = _workShowRepo.GetQueryable(x => x.ISDELETE == false);
            result.TotalCount = model.Count();

            if (!string.IsNullOrEmpty(input.LINE_CODE))
            {
                model = model.Where(x => x.LINE_CODE.Contains(input.LINE_CODE));
            }
            if (!string.IsNullOrEmpty(input.LINE_NAME))
            {
                model = model.Where(x => x.LINE_NAME.Contains(input.LINE_NAME));
            }
            if (!string.IsNullOrEmpty(input.WORKSHOP_ID))
            {
                model = model.Where(x => x.WORKSHOP_ID==input.WORKSHOP_ID);
            }
            var mapper = _mapper.Map<List<GetProductionLineOutPut>>(model.ToList());
            foreach (var item in mapper)
            {
               // var name = WorkShop.Where(x => x.ID == item.WORKSHOP_ID).First();
                var name=_workShowRepo.GetEntity(x=>x.ID==item.WORKSHOP_ID);
                if (name != null)
                {
                    item.WORKSHOP_NAME = name.WORKSHOP_NAME;
                }
            }
            result.PageDatas = mapper.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            result.TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / input.PageSize);
            result.Code = ApiCode.Success;
            result.Message = "成功";
            return result;
        }
        /// <summary>
        /// 工位新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddWorkStation(AddWorkStationInput input)
        {
            var result = new ApiResult<int>();
            var isExist = _workStationRepo.GetList().Where(x => x.WORKSTATION_CODE == input.WORKSTATION_CODE).Any();
            if (isExist)
            {
                result.Code = ApiCode.ParameterError;
                result.Message = "工位编码已存在";
                return result;
            }
            var data = _mapper.Map<SLY_WORKSTATION_MANAGEMENTMODEL>(input);
            data.ID = YitIdHelper.NextId().ToString();
            data.CREATETIME = DateTime.Now;
            data.ISDELETE = false;
            data.CREATEDUSERNAME = "admin"; // 假设创建者为admin
            var r= _workStationRepo.Add(data);
            if(r>0)
            {
                result.Code = ApiCode.Success;
                result.ResultData = r;
                result.Message = "添加成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.ParameterError;
                result.Message = "添加失败";
                return result;

            }
        }
        /// <summary>
        /// 工位修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateWorkStation(UpdateWorkStationInput input)
        {
            var result = new ApiResult<int>();
            var workstation = _workStationRepo.GetEntity(x => x.ID == input.ID);
            if (workstation == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "工位不存在";
                return result;
            }
            var model = _mapper.Map<SLY_WORKSTATION_MANAGEMENTMODEL>(input);
           model.UPDATEDUSERNAME = "admin";
            model.UPDATETIME = DateTime.Now;
            model.CREATEDUSERNAME = "admin";
            model.CREATETIME = workstation.CREATETIME;

            int r=_workStationRepo.Update(model);
            if(r>0)
            {
                result.Code = ApiCode.Success;
                result.Message = "修改工位成功";
                result.ResultData = r;
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "修改工位失败";
                return result;
            }
        }
        /// <summary>
        /// 工位删除
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteWorkStation(string ID)
        {
            var result = new ApiResult<int>();
            var data = _workStationRepo.GetEntity(x => x.ID == ID);
            if(data == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "数据不存在";
                return result;
            }
            data.ISDELETE= true;
             int r=   _workStationRepo.Update(data);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "删除成功";
                result.ResultData = r;
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "删除失败";
                result.ResultData = r;
                return result;
            }
        }
        /// <summary>
        /// 工位列表显示
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<GetWorkStationOutPut>> GetWorkStation()
        {
           var result= new ApiResult<List<GetWorkStationOutPut>>();
            var list = _workStationRepo.GetList().Where(x => x.ISDELETE == false).ToList();
            var r = _mapper.Map<List<GetWorkStationOutPut>>(list);
            result.ResultData = r;
            result.Code = ApiCode.Success;
            result.Message = "查询成功";
            return result;
        }
        /// <summary>
        /// 查询所有工位分页列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<GetWorkStationOutPut> GetPageWorkStation(SearchWorkStationInput input)
        {
            var result = new ApiPaging<GetWorkStationOutPut>();
            var model = _workStationRepo.GetQueryable(x => x.ISDELETE == false);
           
            if (input.WORKSTATION_CODE != null)
            {
                model = model.Where(x => x.WORKSTATION_CODE.Contains(input.WORKSTATION_CODE));
            }
            if (input.WORKSTATION_NAME != null)
            {
                model = model.Where(x => x.WORKSTATION_NAME.Contains(input.WORKSTATION_NAME));
            }
            if (input.LINE_ID != null)
            {
                model = model.Where(x => x.LINE_ID.Contains(input.LINE_ID));
            }
            var modellist=_mapper.Map<List<GetWorkStationOutPut>>(model.ToList());
            foreach (var item in modellist)
            {
                var line = _productionlineRepo.GetEntity(x => x.ID == item.LINE_ID);
                if (line != null)
                {
                    item.LINE_NAME = line.LINE_NAME;
                }
            }
            result.TotalCount = model.Count();
            result.TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / input.PageSize);
            result.PageDatas = modellist.OrderByDescending(x=>x.ID).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            result.Code = ApiCode.Success;
            result.Message = "查询工单列表";
            return result;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> WorkStationDeleteList(List<string> Ids)
        {
            var result = new ApiResult<int>();
            foreach (var item in Ids)
            {
                var id=_workStationRepo.GetEntity(x=>x.ID==item);
                id.ISDELETE = true;
                _workStationRepo.Update(id);
            }
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = 1,
                Message = "删除成功"
            };
        }
        /// <summary>
        /// 客户添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddCustomer(AddCustomerInput input)
        {
            var result=new ApiResult<int>();
            var model = _mapper.Map<SLY_CUSTOMER_MANAGEMENTMODEL>(input);
            model.CREATEDUSERNAME = "admin";
            model.CREATETIME = DateTime.Now;
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            int r = _customerRepo.Add(model);
            if(r>0)
            {
                result.Code = ApiCode.Success;
                result.ResultData = r;
                result.Message = "添加成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "添加失败";
                return result;
            }

        }
        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateCustomer(UpdateCustomerInput input)
        {
          var result=new ApiResult<int>();
            var updateResult = _customerRepo.GetEntity(x => x.ID == input.ID);
            if (updateResult == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "数据不存在";
                return result;
            }
            var list=_mapper.Map<SLY_CUSTOMER_MANAGEMENTMODEL>(input);
            list.CREATEDUSERNAME= updateResult.CREATEDUSERNAME;
            list.UPDATETIME=DateTime.Now;
            list.CREATEDUSERNAME= updateResult.CREATEDUSERNAME;
            list.UPDATEDUSERNAME = "admin"; // 假设更新者为admin
            list.CREATEDUSERNAME = updateResult.CREATEDUSERNAME;
            int r=_customerRepo.Update(list);
            if (r>0)
            {
                result.Code = ApiCode.Success;
                result.ResultData = r;
                result.Message = "修改成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "修改失败";
                return result;
            }
        }
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> DeleteCustomer(string Id)
        {
            var result = new ApiResult<int>();
            var customer=_customerRepo.GetEntity(x=>x.ID==Id);
            customer.ISDELETE=true;
            int r=_customerRepo.Update(customer);
            result.Code=r>0?ApiCode.Success:ApiCode.InternalError;
            result.Message=r>0?"删除成功":"删除失败";
            return result;
        }
        /// <summary>
        /// 获取所有客户信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<GetCustomerOutPut>> GetCustomer()
        {
            var result = new ApiResult<List<GetCustomerOutPut>>();
            var list=_customerRepo.GetList().Where(x => x.ISDELETE == false).ToList();
            var data = _mapper.Map<List<GetCustomerOutPut>>(list);
            result.ResultData = data;
            result.Code = ApiCode.Success;
            result.Message = "查询成功";
            return result;

        }
        /// <summary>
        /// 查询所有客户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<GetCustomerOutPut> GetPageCustomer(SearchCustomerInput input)
        {
            var result = new ApiPaging<GetCustomerOutPut>();
            var model = _customerRepo.GetQueryable(x => x.ISDELETE == false);
            if(input.CUSTOMER_CODE != null)
            {
                model = model.Where(x => x.CUSTOMER_CODE == input.CUSTOMER_CODE);
            }
            if (input.CUSTOMER_NAME != null)
            {
                model = model.Where(x => x.CUSTOMER_NAME == input.CUSTOMER_NAME);
            }
            var list = _mapper.Map<List<GetCustomerOutPut>>(model.ToList());
            result.TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / input.PageSize);
            result.PageDatas = list.OrderByDescending(x => x.ID).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            result.TotalCount = model.Count();
            return result;
        }
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> CustomerDeleteList(List<string> Ids)
        {
           var result = new ApiResult<int>();
            int r = 0;
            foreach (var item in Ids)
            {
                var data = _customerRepo.GetEntity(x => x.ID == item);
                if (data != null)
                {
                    data.ISDELETE = true;
                    r = _customerRepo.Update(data);
                }
            }
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "批量删除成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "批量删除失败";
                return result;
            }

        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddSupplier(AddSupplierInput input)
        {
            var result = new ApiResult<int>();
            if (string.IsNullOrEmpty(input.SUPPLIER_CODE))
            {
                result.Code = ApiCode.ParameterError;
                result.Message = "供应商编码不能为空";
                return result;
            }
            
            var model = _mapper.Map<SLY_SUPPLIER_MANAGEMENTMODEL>(input);
            model.ID = YitIdHelper.NextId().toString();
           model.CREATETIME = DateTime.Now;
            model.ISDELETE = false;
            model.CREATEDUSERNAME = "admin";
            var isExist = _supplierRepo.GetQueryable(x => x.SUPPLIER_CODE == input.SUPPLIER_CODE).Any();
            if (isExist)
            {
                result.Code = ApiCode.ParameterError;
                result.Message = "供应商编码已存在";
                return result;
            }
            int r = _supplierRepo.Add(model);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.ResultData = r;
                result.Message = "添加成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "添加失败";
                return result;
            }
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateSupplier(UpdateSupplierInput input)
        {
            var result=new ApiResult<int>();
            var model = _supplierRepo.GetEntity(x => x.ID == input.ID);
            if (model == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "供应商不存在";
                return result;
            }
            var updateModel = _mapper.Map<SLY_SUPPLIER_MANAGEMENTMODEL>(input);
            updateModel.UPDATETIME = DateTime.Now;
            updateModel.CREATETIME = model.CREATETIME;
            updateModel.CREATEDUSERNAME = model.CREATEDUSERNAME; // 保持创建用户名不变
            updateModel.UPDATEDUSERNAME = "admin";
            
            int r = _supplierRepo.Update(updateModel);
            result.Code = r > 0 ? ApiCode.Success : ApiCode.InternalError;
            result.Message = r > 0 ? "修改成功" : "修改失败";
            result.ResultData = r;
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> DeleteSupplier(string Id)
        {
           var result=new ApiResult<int>();
            var isExist = _supplierRepo.GetEntity(x => x.ID == Id);
            if (isExist == null)
            {
                result.Code = ApiCode.NotFound;
                result.Message = "数据不存在";
                return result;
            }
            isExist.ISDELETE = true;
           int r= _supplierRepo.Update(isExist);
            if(r>0)
            {
               result.Code = ApiCode.Success;
                result.Message = "删除成功";
                result.ResultData = r;
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "删除失败";
                result.ResultData = r;
                return result;
            }
        }
        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<GetSupplierOutPut>> GetSupplier()
        {
            var result = new ApiResult<List<GetSupplierOutPut>>();
            var datalist = _supplierRepo.GetList().Where(x=>x.ISDELETE==false).ToList(); 
            var list= _mapper.Map<List<GetSupplierOutPut>>(datalist);
            result.ResultData = list;
            result.Code = ApiCode.Success;
            result.Message = "查询成功";
            return result;
        }
        /// <summary>
        /// 供应商查询分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<GetSupplierOutPut> GetPageSupplier(SearchSupplierInput input)
        {
            var result = new ApiPaging<GetSupplierOutPut>();
            var model = _supplierRepo.GetQueryable(x => x.ISDELETE == false);
            if (input.SUPPLIER_CODE != null)
            {
                model = model.Where(x => x.SUPPLIER_CODE.Contains(input.SUPPLIER_CODE));
            }
            if (input.SUPPLIER_NAME != null)
            {
                model = model.Where(x => x.SUPPLIER_NAME.Contains(input.SUPPLIER_NAME));
            }
            if (input.SUPPLIER_ADDRESS != null)
            {
                model = model.Where(x => x.SUPPLIER_ADDRESS.Contains(input.SUPPLIER_ADDRESS));
            }
          var list = _mapper.Map<List<GetSupplierOutPut>>(model.ToList());
           list = list.OrderByDescending(x => x.ID).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            return new ApiPaging<GetSupplierOutPut>()
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() / (double)input.PageSize),
                PageDatas = list
            };
        }
        /// <summary>
        /// 批删供应商
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> DeleteSupplierList(List<string> Ids)
        {
            var result = new ApiResult<int>();
            foreach (var item in Ids)
            {
                var data = _supplierRepo.GetEntity(x => x.ID == item);
                if (data != null)
                {
                    data.ISDELETE = true;
                    _supplierRepo.Update(data);
                }
            }
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = 1,
                Message = "删除成功"
            };

        }
        /// <summary>
        /// 车间列表求条数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<SLY_WORKSHOP_MANAGEMENTMODEL>> GetListCount()
        {
            var result=new ApiResult<List<SLY_WORKSHOP_MANAGEMENTMODEL>>();
            var data = _workShowRepo.GetList();
            return new ApiResult<List<SLY_WORKSHOP_MANAGEMENTMODEL>>
            {
                Code = ApiCode.Success,
                ResultData = data
            };
        }
        /// <summary>
        /// 产线列表求条数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<SLY_PRODUCTIONLINE_MANAGEMENTMODEL>> GetProductionLineCount()
        {
            var result = new ApiResult<List<SLY_PRODUCTIONLINE_MANAGEMENTMODEL>>();
            var data = _productionlineRepo.GetList();
            return new ApiResult<List<SLY_PRODUCTIONLINE_MANAGEMENTMODEL>>
            {
                Code = ApiCode.Success,
                ResultData = data
            };
        }
        /// <summary>
        /// 工位列表求条数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<SLY_WORKSTATION_MANAGEMENTMODEL>> GetWorkStationCount()
        {
            var result = new ApiResult<List<SLY_WORKSTATION_MANAGEMENTMODEL>>();
            var data = _workStationRepo.GetList();
            return new ApiResult<List<SLY_WORKSTATION_MANAGEMENTMODEL>>
            {
                Code = ApiCode.Success,
                ResultData = data
            };
        }
        /// <summary>
        /// 客户列表求条数
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<SLY_CUSTOMER_MANAGEMENTMODEL>> GetCustomerCount()
        {
            var result = new ApiResult<List<SLY_CUSTOMER_MANAGEMENTMODEL>>();
            var data = _customerRepo.GetList();
            return new ApiResult<List<SLY_CUSTOMER_MANAGEMENTMODEL>>
            {
                Code = ApiCode.Success,
                ResultData = data
            };
        }
        /// <summary>
        /// 供应商列表求条数
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<SLY_SUPPLIER_MANAGEMENTMODEL>> GetSupplierCount()
        {
            var result = new ApiResult<List<SLY_SUPPLIER_MANAGEMENTMODEL>>();
            var data = _supplierRepo.GetList();
            return new ApiResult<List<SLY_SUPPLIER_MANAGEMENTMODEL>>
            {
                Code = ApiCode.Success,
                ResultData = data
            };
        }
        #endregion
        /// <summary>
        /// 联系列表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ApiPaging<GetRelstionOutPut> GetRelstion(string? ID)
        {
            var result = new ApiPaging<GetRelstionOutPut>();
            var model = _relationRepo.GetQueryable(x => x.ISDELETE == false&&x.CUSTOMER_ID==ID);
            var list = _mapper.Map<List<GetRelstionOutPut>>(model.ToList());
            return new ApiPaging<GetRelstionOutPut>()
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() / (double)10),
                PageDatas = list
            };
        }
        /// <summary>
        /// 收款列表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ApiPaging<GetCollectionOutPut> GetCollection(string? ID)
        {
            var result = new ApiPaging<GetCollectionOutPut>();
            var model = _collectionRepo.GetQueryable(x => x.ISDELETE == false && x.CUSTOMER_ID == ID);
            var list = _mapper.Map<List<GetCollectionOutPut>>(model.ToList());
            return new ApiPaging<GetCollectionOutPut>()
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() / (double)10),
                PageDatas = list
            };
        }
        /// <summary>
        /// 销售列表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ApiPaging<GetSalesOutPut> GetSales(string? ID)
        {
            var result = new ApiPaging<GetSalesOutPut>();
            var model = _salesnRepo.GetQueryable(x => x.ISDELETE == false && x.CUSTOMER_ID == ID);
            var list = _mapper.Map<List<GetSalesOutPut>>(model.ToList());
            return new ApiPaging<GetSalesOutPut>()
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() / (double)10),
                PageDatas = list
            };
        }
        /// <summary>
        /// 发票列表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ApiPaging<GetInvoiceOutPut> GetInvoice(string? ID)
        {
            var result = new ApiPaging<GetInvoiceOutPut>();
            var model = _involcenRepo.GetQueryable(x => x.ISDELETE == false && x.CUSTOMER_ID == ID);
            var list = _mapper.Map<List<GetInvoiceOutPut>>(model.ToList());
            return new ApiPaging<GetInvoiceOutPut>()
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() / (double)10),
                PageDatas = list
            };
        }
        /// <summary>
        /// 出库列表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ApiPaging<GetDeliveryOutPut> GetDelivery(string? ID)
        {
            var result = new ApiPaging<GetDeliveryOutPut>();
            var model = _deliveryRepo.GetQueryable(x => x.ISDELETE == false && x.CUSTOMER_ID == ID);
            var list = _mapper.Map<List<GetDeliveryOutPut>>(model.ToList());
            return new ApiPaging<GetDeliveryOutPut>()
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() / (double)10),
                PageDatas = list
            };
        }
        /// <summary>
        /// 联系列表添加
        /// </summary>
        /// <param name="addRelationInput"></param>
        /// <returns></returns>
        public ApiResult<int> AddRelation(AddRelationInput addRelationInput)
        {
            var result = new ApiResult<int>();
            var model = _mapper.Map<SLY_RELATION_RECORDMODEL>(addRelationInput);
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _relationRepo.Add(model),
                Message = "添加成功"
            };
        }
        /// <summary>
        /// 销售列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public ApiResult<int> AddSales(AddSalesInput Input)
        {
            var result = new ApiResult<int>();
            var model = _mapper.Map<SLY_SALES_RECORDMODEL>(Input);
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _salesnRepo.Add(model),
                Message = "添加成功"
            };
        }
        /// <summary>
        /// 联系列表求条数
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<GetRelstionOutPut>> GetRelstionCount()
        {
            var result = new ApiResult<List<GetRelstionOutPut>>();
            var model = _relationRepo.GetList();
            return new ApiResult<List<GetRelstionOutPut>>
            {
                Code = ApiCode.Success,
                ResultData = _mapper.Map<List<GetRelstionOutPut>>(model)
            };
           
        }
        /// <summary>
        /// 销售列表求条数
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<GetSalesOutPut>> GetSalesCount()
        {
            var result = new ApiResult<List<GetSalesOutPut>>();
            var model = _salesnRepo.GetList();
            return new ApiResult<List<GetSalesOutPut>>
            {
                Code = ApiCode.Success,
                ResultData = _mapper.Map<List<GetSalesOutPut>>(model)
            };
        }
        /// <summary>
        /// 发票列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddInvoice(AddInvoice Input)
        {
           var result = new ApiResult<int>();
            var model = _mapper.Map<SLY_INVOICE_RECORD>(Input);
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _involcenRepo.Add(model),
                Message = "添加成功"
            };
        }
        /// <summary>
        /// 出库列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddDelicery(AddDelicery Input)
        {
            var result = new ApiResult<int>();
            var model = _mapper.Map<SLY_DELIVERY_RECORD>(Input);
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _deliveryRepo.Add(model),
                Message = "添加成功"
            };
        }
        /// <summary>
        /// 收款列表添加
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddCollection(AddCollection Input)
        {
            var result = new ApiResult<int>();
            var model = _mapper.Map<SLY_COLLECTION_RECORD>(Input);
            model.ID = YitIdHelper.NextId().ToString();
            model.ISDELETE = false;
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _collectionRepo.Add(model),
                Message = "添加成功"
            };
        }
        /// <summary>
        /// 物料列表添加
        /// </summary>
        /// <param name="addMaterialInput"></param>
        /// <returns></returns>
        public ApiResult<int> AddMaterial(Input.Material.AddMaterialInput addMaterialInput)
        {
            var result = new ApiResult<int>();
            var model = _mapper.Map<BomMaterialIdModel>(addMaterialInput);
            model.Id = YitIdHelper.NextId().ToString();
            model.IsDeleted = false;
            model.CreatedAt = DateTime.Now;
            model.CreatedBy = "admin";
            //中间表
            Parent_ChildrenModel bommaterial = new Parent_ChildrenModel();
            bommaterial.Id = YitIdHelper.NextId().ToString();
            bommaterial.MaterialId = model.Id;
            bommaterial.BomId = addMaterialInput.BomId;
            PcRepository.Add(bommaterial);
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _materialRepo.Add(model),
                Message = "添加成功"
            };
           
        }
        /// <summary>
        /// 批量删除物料
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ApiResult<int> MaterialDeleteList(List<string> Ids)
        {
            var result=new ApiResult<int>();
            var model = _materialRepo.GetList();
            foreach (var item in Ids)
            {
                var id=model.FirstOrDefault(x => x.Id == item)!;
                id.IsDeleted = true;
                _materialRepo.Update(id);
            }
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = 1,
                Message = "删除成功"
            };
        }
        /// <summary>
        /// 物料列表查询分页
        /// </summary>
        /// <param name="searchMaterialInput"></param>
        /// <returns></returns>
        public ApiPaging<GetMaterialOutPut> GetMaterial(Input.Material.SearchMaterialInput searchMaterialInput)
        {
           var result=new ApiPaging<List<GetMaterialOutPut>>();
            var model = _materialRepo.GetQueryable(x=>x.IsDeleted==false);
            if (!string.IsNullOrEmpty(searchMaterialInput.MaterialName))
            {
                model = model.Where(x => x.MaterialName.Contains(searchMaterialInput.MaterialName));
            }
            return new ApiPaging<GetMaterialOutPut>
            {
                TotalCount = model.Count(),
                TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / searchMaterialInput.PageSize),
                PageDatas = _mapper.Map<List<GetMaterialOutPut>>(model.OrderByDescending(x => x.CreatedAt).Skip((searchMaterialInput.PageIndex - 1) * searchMaterialInput.PageSize).Take(searchMaterialInput.PageSize).ToList())
            };
        }
        /// <summary>
        /// 物料修改
        /// </summary>
        /// <param name="editMaterialInput"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateMaterial(Input.Material.EditMaterialInput editMaterialInput)
        {
           var result=new ApiResult<int>();
            var model = _materialRepo.GetQueryable(X=>X.Id==editMaterialInput.Id).First();
            var data=_mapper.Map<BomMaterialIdModel>(editMaterialInput);
           data.CreatedAt=model.CreatedAt;
            data.CreatedBy=model.CreatedBy;
            data.UpdatedAt=DateTime.Now;
            data.UpdatedBy="admin";
            int r = _materialRepo.Update(data);
            if (r > 0)
            {
                result.Code = ApiCode.Success;
                result.Message = "修改成功";
                return result;
            }
            else
            {
                result.Code = ApiCode.InternalError;
                result.Message = "修改失败";
                return result;
            }
           

        }
        /// <summary>
        /// 获取物料数量
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<GetMaterialOutPut>> GetMaterialcount()
        {
            var result=new ApiResult<List<GetMaterialOutPut>>();
            var data=_materialRepo.GetList();
           var list= _mapper.Map<List<GetMaterialOutPut>>(data);
            return new ApiResult<List<GetMaterialOutPut>>
            {
                Code = ApiCode.Success,
               ResultData=list
            };
        }
        /// <summary>
        /// 获取物料列表
        /// </summary>
        /// <param name="getBomOutPut"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
       public  ApiPaging<GetBomOutPut> GetBom(SearchBomInput input)
        {
          var result=new ApiPaging<GetBomOutPut>();
            var list = _bomRepo.GetList().Where(x => x.IsDeleted == false);
            if (!string.IsNullOrEmpty(input.BomName))
            {
                list = list.Where(x => x.BomName.Contains(input.BomName));
            }
            if (!string.IsNullOrEmpty(input.ProductCode))
            {
                list = list.Where(x => x.ProductCode.Contains(input.ProductCode));
            }
            result.TotalCount=list.Count();
            result.TotalPage=(int)Math.Ceiling(list.Count()*1.0/input.PageSize);
            result.PageDatas=_mapper.Map<List<GetBomOutPut>>(list);
            result.Code=ApiCode.Success;
            result.Message="查询成功";
            return result;

        }
        /// <summary>
        /// 添加bom
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddBom(AddBomInput input)
        {
            var result=new ApiResult<int>();
            var data=_mapper.Map<BomModel>(input);
            data.Id=YitIdHelper.NextId().ToString();
            data.CreatedBy="admin";
            data.CreatedAt=DateTime.Now;
            data.ProductCode = YitIdHelper.NextId().ToString();
            return new ApiResult<int>
            {
                Code = ApiCode.Success,
                ResultData = _bomRepo.Add(data),
                Message = "添加成功"
            };
        }
        /// <summary>
        /// 查询Bom数量
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<GetBomOutPut>> GetBomcount()
        {
            var result=new  List<GetBomOutPut>();
            var list=_bomRepo.GetList();
            return new ApiResult<List<GetBomOutPut>>
            {
                Code = ApiCode.Success,
                ResultData = _mapper.Map<List<GetBomOutPut>>(list)
            };
        }
        /// <summary>
        /// 树形Bom
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<MESTreeBomDTO>> MESTreeBom()
        {

            // 只查最近30天的BOM
            var allBoms = _bomRepo.GetQueryable(x => x.IsDeleted == false).ToList();
            var bomIds = allBoms.Select(b => b.Id).ToList();

            // 只查相关父子关系
            var relations = PcRepository.GetQueryable(r => bomIds.Contains(r.BomId)).ToList();
            var materialIds = relations.Select(r => r.MaterialId).Distinct().ToList();
            // 只查相关物料
            var allMaterials = _materialRepo.GetQueryable(m => materialIds.Contains(m.Id)).ToList();

            // 构建父ID -> 子ID 列表映射（BOM ID -> Material ID）
            var parentToMaterials = relations
                .GroupBy(r => r.BomId)
                .ToDictionary(g => g.Key, g => g.Select(r => r.MaterialId).ToList());

            var result = new List<MESTreeBomDTO>();

            foreach (var bom in allBoms)
            {
                var dto = new MESTreeBomDTO
                {
                    Bom = bom,
                    Children = new List<BomMaterialIdModel>()
                };

                if (parentToMaterials.TryGetValue(bom.Id, out var mIds))
                {
                    // 查找该 BOM 对应的所有子级物料
                    dto.Children = allMaterials
                        .Where(m => mIds.Contains(m.Id))
                        .ToList();
                }

                result.Add(dto);
            }

            return new ApiResult<List<MESTreeBomDTO>>
            {
                Code = ApiCode.Success,
                ResultData = result,
                Message = "查询成功(仅近30天数据)"
            };
        }
    }
}

